package com.zrrd.controller;

import com.zrrd.common.dto.LoginDto;
import com.zrrd.common.vo.ManagerLoginVo;
import com.zrrd.service.ManagerService;
import com.zrrd.common.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author WanJl
 * @version 1.0
 * @title LoginController
 * @description
 * @create 2024/9/6
 */
@RestController
@RequestMapping("/admin")
@ResponseBody
@Slf4j
public class LoginController {

    @Resource
    private ManagerService managerService;

    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 管理员用户登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto loginDto){//DTO 数据传输对象
        ManagerLoginVo managerLoginVo = managerService.login(loginDto);
        return ResponseResult.defaultResponse(managerLoginVo,"登录成功","登录失败");

    }
    @PostMapping("/logout")
    public ResponseResult logout(HttpServletRequest request){
        String token = request.getHeader("token");
        log.info("退出登录了......jwt为"+token);
        Boolean flag = redisTemplate.delete("TOKEN:" + token);
        return ResponseResult.deleteResponse(flag);
    }


}
